<?php
session_start();
require_once( 'student_db.php' );
 
$m_db = new M_Student();
$user_id = $_SESSION['userid'];
$Year = $m_db->getCurrentYear();
$Term = $m_db->getCurrentTerm();

if (isset($_POST['m'])  &&  $_POST['m']!=""){

	if ($_POST['m']=='load_old'){
	  $cid = $_POST['cid'];
	  $r = $_POST['r'];
	  $s = $_POST['s'];
	  $n = $_POST['n'];
	  $co = $_POST['co'];
	  if (isset($_POST['y'])){
		$year = $_POST['y'];
	  }else{
		$year =$Year;
	  }
	  
      $result=$m_db->loadStudent($co,$n,$cid,$r,$s,$year);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
			$today = date("Y-m-d");	
			$year = date("Y");
            while($row = $result->FetchRow()){

               $data[] = array(0,$row['student_id'],$row['code'], $row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname']  
			   ,$row['thai_nickname'],$row['class_name'].'/'.$row['room'],$row['create_date'],$row['register_date'], $row['status_name']  ) ;
			   
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);	
    }else if ($_POST['m']== 'load'){
	  $cid = $_POST['cid'];
	  $r = $_POST['r'];
	  $s = $_POST['s'];
	  $n = $_POST['n'];
	  $co = $_POST['co'];
	  if (isset($_POST['y'])){
		$year = $_POST['y'];
	  }else{
		$year =$Year;
	  }
	  
      $result=$m_db->loadStudent($co,$n,$cid,$r,$s,$year);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
			$today = date("Y-m-d");	
			$year = date("Y");
            while($row = $result->FetchRow()){

               $data[] = array(0,$row['student_id'],$row['code'], $row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname']  
			   ,$row['thai_nickname'],$row['congenital_disease'] ,$row['food_allergies'], $row['status_name']  ) ;
			   
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
    }else   if ($_POST['m']== 'load_invoice'){
	  $cid = $_POST['cid'];
	  $r = $_POST['r'];
	  $n = $_POST['n'];
	  $co = $_POST['co'];
	  $status = $_POST['s'];
	  $year =$Year;
  
      $result=$m_db->loadInvoice($co,$n,$cid,$r,$year,$Term,$status );
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
			$today = date("Y-m-d");	
			$year = date("Y");
            while($row = $result->FetchRow()){

               $data[] = array(0,$row['student_id']
			   , $row['year'].'/'.$row['term']
			   , $row['code']
			   , $row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname'] ." (".$row['thai_nickname'].") "
			   , $row['class_name'] .' / '.  $row['room'] 
			   , number_format($row['fee_term_amt'],2,'.',',')
			   , number_format($row['fee_other_amt'],2,'.',',')
			   , number_format($row['fee_term_amt'] + $row['fee_other_amt'],2,'.',',')
			   , ($row['fee_term_amt'] + $row['fee_other_amt'])==($row['fee_term_amt_pay'] + $row['fee_other_amt_pay'])?1:0 ) ;
			   
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
    }else if ($_POST['m']=='exit'){ 
		$student_id=  $_POST['id'];
		$reason = $_POST['r'];
		$m_db->updateStatusExit($student_id,5,$reason,$user_id );
		echo 1;
		
	}else if ($_POST['m']=='move'){
		$student_id=  $_POST['id'];
		$reason = $_POST['r'];
		$room = $_POST['room'];
		$reason = 'ย้ายห้อง : ' .$reason;
		$m_db->updateRoom($student_id, $room, $reason,$user_id);
		
	 echo 1;
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		$list = $_POST['list'];
		foreach($list as $val){
			$ar['student_id'] = $val['sid'];
			$ar['class_id']=$val['cid'];
			$ar['room']=$val['r'];
			$ar['year']=  $Year;
			
			$m_db->updateStudentStatus($val['sid'], 3, $user_id);
			$m_db->AddStudentToRoom($ar);
		}
		echo 1;
		
    }else if ($_POST['m']=='del_student'){
		$id = $_POST['id'];
		$m_db->deleteStudent($id);
		
	}  
	else if ($_POST['m'] == 'updateTerm'){
		$year = $_POST['year'];
		$term = $_POST['term'];
		
		//if ($year != $m_db->getCurrentYear() || $term != $m_db->getCurrentTerm()){
			$m_db->UpdateSystemConfigYear('CURR_YEAR', $year-543 );
			$m_db->UpdateSystemConfigYear('CURR_TERM',$term);
			$m_db->UpdateTerm($year ,$term);
		//}
	}
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if ($_GET['m'] == 'class'){
			echo json_encode( $m_db->getClassLevel());
		}else if ($_GET['m']=='room'){
			echo  json_encode( $m_db->load_all_room($_GET['i']));
		}else if ($_GET['m']=='status'){
			echo  json_encode($m_db->getStudentStatus());
		}else if ($_GET['m'] =='year'){
		  echo json_encode($m_db->getYear_Term());
		}else if ($_GET['m'] =='term'){
		  echo json_encode($m_db->getTerm());
		}else if ($_GET['m'] =='curryear'){
			echo $m_db->getCurrentYear();
		}
	} 
   
}
 
?>